package controller

import (
	"common/helper"
	myredis "common/redis"
	myUserHelp "common/userHelp"
	"errors"
	"fmt"
	"github.com/valyala/fasthttp"
	"member/model"
	"time"
)

type DailyRunningReturnItem struct {
	DailyRunningReturn float64 `json:"DailyRunningReturn" db:"DailyRunningReturn" cbor:"DailyRunningReturn"`
	TodayRunning       float64 `json:"TodayRunning" db:"TodayRunning" cbor:"TodayRunning"`
	YesterDayRunning   float64 `json:"YesterDayRunning" db:"YesterDayRunning" cbor:"YesterDayRunning"`
}

type DailyRunningRecordItem struct {
	SlotRunning   string `json:"slots_running" db:"slots_running" cbor:"slots_running"`
	PesRunning    string `json:"pes_running" db:"pes_running" cbor:"pes_running"`
	CasRunning    string `json:"cas_running" db:"cas_running" cbor:"cas_running"`
	BingoRunning  string `json:"bingo_running" db:"bingo_running" cbor:"bingo_running"`
	JogoRunning   string `json:"jogo_running" db:"jogo_running" cbor:"jogo_running"`
	EsportRunning string `json:"esport_running" db:"esport_running" cbor:"esport_running"`
}

func (that MemberController) AchieveDailyRunningReturn(ctx *fasthttp.RequestCtx) {
	valUid := model.GetUidFromToken(ctx)
	if valUid == "" {
		helper.Print(ctx, false, errors.New(helper.AccessTokenExpires))
		return
	}

	lockKey := valUid + "DailyRunningReturn"
	if !myredis.Lock(model.GetRedisInstance(), lockKey) {
		helper.GetloggerInstance().Criticalf("lockError:%s", lockKey)
		return
	}

	defer myredis.Unlock(model.GetRedisInstance(), lockKey)
	var DailyRunningReturn float64
	sqlSelect := fmt.Sprintf("select daily_bet_return from tbl_member_return where uid=%s", valUid)
	selectErr := model.GetMasterDBInstance().Get(&DailyRunningReturn, sqlSelect)

	if selectErr != nil {
		helper.InfoLog("[DailyRunningReturn]:%s error:%s", sqlSelect, selectErr.Error())
	}

	if DailyRunningReturn == 0.0 {
		helper.Print(ctx, true, DailyRunningReturn)
		return
	}

	//增加打码
	sqlUpdate := fmt.Sprintf("update tbl_member_return set  daily_bet_return = 0.0 where uid=%s", valUid)
	_, selectErr = model.GetMasterDBInstance().Exec(sqlUpdate)
	if selectErr != nil {
		helper.InfoLog("[tbl_member_return:exec]:%s error:%s", sqlUpdate, selectErr.Error())
	}

	myUserHelp.AddBalanceTransactionRecord(valUid, DailyRunningReturn,
		helper.TransactionDailyBetReturn, "0", helper.GenId(), 0.00, "", "", 0)
	myUserHelp.AddUserWageItem(valUid, DailyRunningReturn, helper.TransactionDailyBetReturn)
	myUserHelp.AddTotalDailyBetReturn(valUid, DailyRunningReturn)

	if DailyRunningReturn > 0.0 {
		_, errExec := model.GetMasterDBInstance().Exec("CALL tbl_report_user_change_update(?,?,?,?)", valUid,
			helper.TransactionDailyBetReturn, DailyRunningReturn, time.Now().Unix())
		if errExec != nil {
			helper.GetloggerInstance().Emergencyf("CALL tbl_report_user_change_update(%s,%d,%0.2f,%d);	reason:%s",
				valUid, helper.TransactionDailyBetReturn, DailyRunningReturn, time.Now().Unix(), errExec.Error())
		}
	}
	myUserHelp.AddUserBalanceByFloat64(valUid, DailyRunningReturn)
	helper.Print(ctx, true, DailyRunningReturn)
}

func (that MemberController) QueryDailyRunningReturn(ctx *fasthttp.RequestCtx) {
	valUid := model.GetUidFromToken(ctx)

	var tReturnItem DailyRunningReturnItem
	sqlSelect := fmt.Sprintf("select daily_bet_return from tbl_member_return where uid=%s", valUid)
	selectErr := model.GetMasterDBInstance().Get(&tReturnItem.DailyRunningReturn, sqlSelect)
	if selectErr != nil {
		helper.InfoLog("[QueryDailyRunningReturn:DailyRunningReturn]:%s error:%s", sqlSelect, selectErr.Error())
	}

	now := time.Now()
	sqlSelect = fmt.Sprintf("select IFNULL(sum(valid_waged),0)  from tbl_report_user_daily where uid=%s and created_at='%s' ",
		valUid, now.Format("2006-01-02"))
	selectErr = model.GetMasterDBInstance().Get(&tReturnItem.TodayRunning, sqlSelect)
	if selectErr != nil {
		helper.InfoLog("[QueryDailyRunningReturn:TodayRunning]:%s error:%s", sqlSelect, selectErr.Error())
	}

	yesterday := now.AddDate(0, 0, -1)
	sqlSelect = fmt.Sprintf("select IFNULL(sum(valid_waged),0)  from tbl_report_user_daily where uid=%s and  created_at='%s'",
		valUid, yesterday.Format("2006-01-02"))

	selectErr = model.GetSlaveDBInstance().Get(&tReturnItem.YesterDayRunning, sqlSelect)
	if selectErr != nil {
		helper.InfoLog("[QueryDailyRunningReturn:YesterDayRunning]:%s error:%s", sqlSelect, selectErr.Error())
	}

	helper.Print(ctx, true, tReturnItem)
}

func (that MemberController) QueryDailyRunningRecord(ctx *fasthttp.RequestCtx) {
	valUid := model.GetUidFromToken(ctx)

	now := time.Now()
	var RunningRecord []DailyRunningRecordItem
	sqlSelect := fmt.Sprintf("select IFNULL(sum(slots_running),0) as slots_running,"+
		"IFNULL(sum(pes_running),0) as pes_running,"+
		"IFNULL(sum(cas_running),0) as cas_running, "+
		"IFNULL(sum(bingo_running),0) as bingo_running,"+
		"IFNULL(sum(jogo_running),0) as jogo_running,"+
		"IFNULL(sum(esport_running),0) as  esport_running  "+
		"from tbl_report_user_daily where uid=%s and created_at='%s'",
		valUid, now.Format("2006-01-02"))
	selectErr := model.GetSlaveDBInstance().Select(&RunningRecord, sqlSelect)
	if selectErr != nil {
		helper.InfoLog("[QueryDailyRunningRecord:RunningRecord]:%s error:%s", sqlSelect, selectErr.Error())
	}
	helper.Print(ctx, true, RunningRecord)
}

func (that MemberController) QueryYesterdayRunningRecord(ctx *fasthttp.RequestCtx) {
	valUid := model.GetUidFromToken(ctx)

	now := time.Now()
	yesterday := now.AddDate(0, 0, -1)

	var RunningRecord []DailyRunningRecordItem
	sqlSelect := fmt.Sprintf("select IFNULL(sum(slots_running),0) as slots_running,"+
		"IFNULL(sum(pes_running),0) as pes_running,"+
		"IFNULL(sum(cas_running),0) as cas_running, "+
		"IFNULL(sum(bingo_running),0) as bingo_running,"+
		"IFNULL(sum(jogo_running),0) as jogo_running,"+
		"IFNULL(sum(esport_running),0) as  esport_running  "+
		"from tbl_report_user_daily where uid=%s and created_at='%s' ",
		valUid, yesterday.Format("2006-01-02"))
	selectErr := model.GetSlaveDBInstance().Select(&RunningRecord, sqlSelect)
	if selectErr != nil {
		helper.InfoLog("[QueryYesterdayRunningRecord:RunningRecord]:%s error:%s", sqlSelect, selectErr.Error())
	}

	helper.Print(ctx, true, RunningRecord)
}
